<?php

/**
 * Created by Reliese Model.
 */

namespace App\Models;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;

/**
 * Class Branch
 *
 * @property int $id
 * @property string|null $branch_code
 * @property string|null $name
 * @property int|null $pid
 * @property int|null $hid
 * @property float $commission_rate
 * @property float $task
 * @property int $is_important
 * @property string|null $is_effective
 * @property string|null $remark
 * @property float|null $weight
 * @property Carbon|null $create_time
 * @property Carbon|null $update_time
 * @property int|null $results_branch_id
 *
 * @property Collection|ExcelFile[] $excel_files
 * @property Collection|HistoryDatum[] $history_data
 * @property Collection|InterestRecord[] $interest_records
 * @property Collection|OaModel[] $oa_models
 * @property Collection|OaRecord[] $oa_records
 * @property Collection|Order[] $orders
 * @property Collection|ProductClass[] $product_classes
 * @property Collection|User[] $users
 * @property Collection|Worker[] $workers
 *
 * @package App\Models
 */
class Branch extends Model
{
	protected $table = 'branch';

	protected $casts = [
		'pid' => 'int',
		'hid' => 'int',
		'commission_rate' => 'float',
		'task' => 'float',
		'is_important' => 'int',
		'weight' => 'float',
		'results_branch_id' => 'int'
	];

	protected $fillable = [
		'branch_code',
		'name',
		'pid',
		'hid',
		'commission_rate',
		'task',
		'is_important',
		'is_effective',
		'remark',
		'weight',
		'results_branch_id'
	];

    public function excel_files()
    {
        return $this->hasMany(ExcelFile::class);
    }

    public function history_data()
    {
        return $this->hasMany(HistoryDatum::class);
    }

    public function interest_records()
    {
        return $this->hasMany(InterestRecord::class);
    }
    /*从下级找上级*/
    public function branch()
    {
        return $this->belongsTo(Branch::class, 'pid');
    }
    /*从上级找下级*/
    public function branches()
    {
        return $this->hasMany(Branch::class, 'pid');
    }

    public function oa_models()
    {
        return $this->belongsToMany(OaModel::class, 'oa_model_branch')
            ->withPivot('id')
            ->withTimestamps();
    }

    public function oa_records()
    {
        return $this->hasMany(OaRecord::class);
    }

    public function orders()
    {
        return $this->hasMany(Order::class);
    }

    public function users()
    {
        return $this->hasMany(User::class);
    }

    public function workers()
    {
        return $this->belongsToMany(Worker::class, 'worker_branch')
            ->withPivot('id')
            ->withTimestamps();
    }
    public function manage_workers()
    {
        return $this->belongsToMany(Worker::class, 'worker_branch_manage')
            ->withPivot('id')
            ->withTimestamps();
    }

	public function product_classes()
	{
		return $this->belongsToMany(ProductClass::class, 'product_class_branch')
					->withPivot('id')
					->withTimestamps();
	}

}
